% # variable:
% # bookshelf, bookuuid, current_uuid, record_name, info, eznav_level, stnav_level

% import xml.etree.ElementTree as ET

% # 整理並抽取基本資料
% try:
    % book = bookshelf.findbook(bookuuid)
    % bookxml = book.book_xml()
    % ebook = ET.fromstring(bookxml)

    % enote = ebook.find('.//note[@uuid="{}"]'.format(current_uuid))
    % if enote is None and bookuuid == current_uuid:
        % enote = ebook
    % end

    % # enote 存在（有效），則計算與歷史記錄有關的項目
    % if enote is not None:
        % mtime = book.mtime(current_uuid)
        % ehistory = bookshelf.ehistory(bookuuid, current_uuid)
    % end
% except:
    % book = None
    % ebook = None
    % enote = None
% end


<aside id="sidebar">

    <!-- eznav -->
    % if enote is not None:
        % include ez_nav book = book, ebook = ebook, target_uuid = current_uuid, max_level = eznav_level
    % else:
        <ul class="menuroot">
            <li><a href="/last" title="Back to the Last note page which your browsing before." target="">Back to Last Page<span class="notify"><img class="icon" src="/static/icon/sidebar/move.svg"></span></a>
        </ul>
    % end

    <!-- Nav -->
    % ebooks = bookshelf.ebooks()
    % # if len(ebooks) > 0:
        % # include nav bookshelf = bookshelf, ebooks = ebooks, target_uuid = current_uuid, target_bookuuid = bookuuid
        <!--<hr />-->
    % # end

    <ul class="menuroot">
        <li><a href="/index" title="Page Index" target="">Index<span class="notify"><img class="icon" src="/static/icon/sidebar/index.svg"></span></a>
        <ul>
            % for ebook in ebooks:
                % if ebook.get('uuid') == bookuuid:
                    % include nav_item book = bookshelf.findbook(ebook.get('uuid')), eroot = ebook, bookuuid = ebook.get('uuid'), target_uuid = current_uuid, max_level = stnav_level, trace_path_enable = True
                % else:
                    % include nav_item book = bookshelf.findbook(ebook.get('uuid')), eroot = ebook, bookuuid = ebook.get('uuid'), target_uuid = current_uuid, max_level = stnav_level, trace_path_enable = False
                % end
            % end
        </ul>
    </ul>
    <hr />

    <!-- Configure Menu -->
    <ul class="menuroot">
        <li><a href="/config" title="Configure & Manage your LoNote server.">Configure<span class="notify"><img class="icon" src="/static/icon/sidebar/config.svg"></span></a>
            <ul>
                % if bookuuid:
                    <li><a href="/config/book/{{!bookuuid}}" title="Configure & Manage this book.">Configure This Book<span class="notify"><img class="icon" src="/static/icon/sidebar/config.svg"></span></a></li>
                % end
                <li><a href="/static/page/hotkey.html" title="Check Hotkey table." target="_blank">Hot Key<span class="notify"><img class="icon" src="/static/icon/sidebar/hotkey.svg"></span></a></li>
                <!-- <li><a href="/start" title="Beginner start page.">How to Start<span class="notify"><img class="icon" src="/static/icon/sidebar/howtostart.svg"></span></a></li> -->
                <li><a href="{{!info.homepage}}" title="Check about the LoNote Project" target="_blank">About LoNote<span class="notify"><img class="icon" src="/static/icon/sidebar/about.svg"></a></span>
                    <ul>
                        <li><a href="{{!info.changelogpage}}" target="_blank"title="Check the LoNote's Changelog">Version: <b>{{!info.version}}</b></a></li>
                        <li><a href="{{!info.bugpage}}" target="_blank" title="Check or submit your bugs or feature request."><b>Bug Report</b></a></li>
                        <li><a href="{{!info.forumpage}}" target="_blank" title="Talk something about LoNote."><b>Forum</b></a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

    <!-- 歷史記錄 -->
    % if enote is not None:
        <hr />
        % include record_menu ehistory = ehistory, mtime = mtime, record_name =record_name
    % end

    <!-- 編輯／瀏覽按鈕 -->
    % if enote is not None:
        <ul class="menuroot">
            <li><a id="content-mode" title="toggle by click"></a>
                <ul>
                    <li><a href="?mode=html" title="Show this page as a complete HTML page include CSS and other things, but no sidebar and edit function.">HTML Mode</a></li>
                    <li><a href="?mode=raw" title='Show only the "pure content" which you edited in this page.'>Raw Mode</a></li>
                </ul>
            </li>
        </ul>
    % end


    <!--功能選單面板-->
    % if enote is not None:
        <hr />
        <div id="cmdpanel" style="width: 160px;">
            <ul class="menuroot">
                <li id="btn_content_update" title="Save as the current version."><a>Save<span class="notify"><span class="response_notify"></span><img class="icon" src="/static/icon/sidebar/save.svg"></span></a>
                    <ul>
                        <li id="btn_title_update" title="Change the title."><a>Change Title<span class="notify"><img class="icon" src="/static/icon/sidebar/changetitle.svg"></span></a></li>
                        <li id="btn_position_update" title="Move the current note position."><a>Move<span class="notify"><img class="icon" src="/static/icon/sidebar/move.svg"></span></a></li>
                        <li id="btn_make_record" title="Freeze the current version as a record."><a>Freeze Version<span class="notify"><span class="response_notify"></span><img class="icon" src="/static/icon/sidebar/version.svg"></span></a></li>
                        <li id="btn_note_delete" title="Delete the current page."><a>Recycle<span class="notify"><img class="icon" src="/static/icon/sidebar/recycle.svg"></span></a></li>
                        <li id="btn_note_export" title="Export the current page (and all sub pages) as a new Notebook."><a>Export<span class="notify"><img class="icon" src="/static/icon/sidebar/export.svg"></span></a></li>
                        <li id="btn_book_import" title="Import a existing notebook as sub-page of current note."><a>Import<span class="notify"><img class="icon" src="/static/icon/sidebar/import.svg"></span></a></li>
                        <li id="btn_note_create" title="Create a new note"><a>New Note<span class="notify"><img class="icon" src="/static/icon/sidebar/newnote.svg"></span></a></li>
                    </ul>
                </li>

                <!-- 資源列表 -->
                <li id="resource_list"><a title="All page resource are listed here...">Resource<span class="notify"><img class="icon" src="/static/icon/sidebar/resource.svg"></span></a>
                    <ul>
                        % resource_list = book.resource_list(current_uuid)
                        % for resource in resource_list:
                            %if resource.get('res_type') == 'emb':
                                %icon = ' <img class="icon" src="/static/icon/sidebar/emb.svg">'
                            %elif resource.get('res_type') == 'att':
                                %icon = ' <img class="icon" src="/static/icon/sidebar/att.svg">'
                            %else:
                                %icon = ''
                            %end
                            <li res_name="{{resource.get('name')}}"><a class="resource_link" href="{{resource.get('name')}}"><span class="notify-small">{{!icon}}</span>{{resource.get('name')}}</a>
                                <ul>
                                    <li onclick="lo.noteop.resource_del_dia('{{resource.get("name")}}');"><a>delete</a></li>
                                </ul>
                            </li>
                        %end
                    </ul>
                </li>
            </ul>
        </div>
    %end


    <script>
        // 掛載 sidebar 所需的功能
        lo.sidebar.ready();
    </script>

</aside>
